package gongju;

import java.sql.*;

public class DBUtil {
    private static String url = "jdbc:mysql:///CompanyManager?characterEncoding=utf8";
    private static String name = "root";
    private static String password = "root";
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    public static Connection getConn() throws SQLException {
        Connection conn = DriverManager.getConnection(url,name,password);
        return conn;
    }
    public static ResultSet qure(String sql , Object...tj) throws SQLException {
        Connection conn = getConn();
        PreparedStatement pre = conn.prepareStatement(sql);
        for (int i = 0; i < tj.length; i++) {
            pre.setObject((i+1),tj[i]);
        }
        ResultSet re = pre.executeQuery();
        return re;
    }
    public static int update(String sql , Object...tj) throws SQLException {
        Connection conn = getConn();
        PreparedStatement pre = conn.prepareStatement(sql);
        int re = 0;
        for (int i = 0; i < tj.length; i++) {
            pre.setObject((i+1),tj[i]);
        }
        re = pre.executeUpdate();
        return re;
    }
    public static void close(ResultSet re , PreparedStatement pre , Connection conn) throws SQLException {
        if (re!=null){
            re.close();
        }
        if (pre!=null){
            pre.close();
        }
        if (conn!=null){
            conn.close();
        }
    }
}
